﻿<div id="dgToolbarBXJGShopOrder" class="datagrid-toolbar1">
    <div class="toolbar_right">
        <!--<input class="easyui-textbox" data-options="width:260, prompt:'名称/助记码/品牌/规格型号/供应商 等...'" />-->
    </div>
</div>
<table id="dgBXJGShopOrder"></table>
<script>
    (function () {
        //在razor变量两边加单引号会被当成字符串，vs智能提示不会消失
        var m = 'BXJGShopOrder';
        var canShipment = abp.auth.isGranted(m + 'Shipment');
        var canRefund = abp.auth.isGranted(m + 'Refund');
        var canCancel = abp.auth.isGranted(m + 'Cancel');
        var $toolbar = $('#dgToolbar' + m);
        var $toolbarRight = $toolbar.find('.toolbar_right');
        var $dg = $('#dg' + m);
        var $cmenu = $('<div />');

        $cmenu.menu();
        if (canShipment) {
            var $btnAdd = $('<a />').insertBefore($toolbarRight);
            var $btnAddOptions = {
                iconCls: 'icon-add',
                text: abp.localization.bxjgShop('发货')
            };
            $btnAdd.linkbutton($.extend($btnAddOptions, {
                plain: true,
                onClick: function () {
                    showForm();
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnAddOptions, {
                onclick: function () {
                    $btnAdd.trigger('click');
                }
            }));
        }
        if (canRefund) {
            var $btnUpdate = $('<a />').insertBefore($toolbarRight);
            var $btnUpdateOptions = {
                iconCls: 'icon-edit',
                text: abp.localization.bxjgShop('退款')
            };
            $btnUpdate.linkbutton($.extend($btnUpdateOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    showForm(rows[0].id);
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnUpdateOptions, {
                onclick: function () {
                    $btnUpdate.trigger('click');
                }
            }));
        }
        if (canCancel) {
            var $btnDelete = $('<a />').insertBefore($toolbarRight);
            var $btnDeleteOptions = {
                iconCls: 'icon-remove',
                text: abp.localization.bxjgShop('取消')
            };
            $btnDelete.linkbutton($.extend($btnDeleteOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.confirm(
                        abp.localization.zlj('DeleteConfirm'),
                        abp.localization.zlj('DeleteConfirmPropt'),
                        function (isConfirmed) {
                            if (isConfirmed) {
                                var ary = new Array();
                                $(rows).each(function () {
                                    ary.push(this.id);
                                });
                                abp.ui.setBusy(null, abp.services.app[abp.utils.toCamelCase(m)].deleteBatch(ary).done(function (d) {
                                    abp.notify.success(abp.localization.zlj('Delete') + abp.localization.zlj('Success') + '!');
                                    $btnRefresh.trigger('click');
                                }));
                            }
                        }
                    );
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnDeleteOptions, {
                onclick: function () {
                    $btnDelete.trigger('click');
                }
            }));
        }

        var $btnRefresh = $('<a />').insertBefore($toolbarRight);
        $btnRefresh.linkbutton({
            iconCls: 'icon-reload',
            text: abp.localization.zlj('Refresh'),
            plain: true,
            onClick: function () {
                $dg.datagrid('reload');
            }
        });

        var $keywordTextbox = $('<input />').appendTo($toolbarRight);
        $keywordTextbox.textbox({
            width: 250,
            prompt: abp.localization.bxjgShop('BXJGShopOrderKeywordsInputPrompt'),
            onChange: search
        });

        var $orderStatus = $('<input />').appendTo($toolbarRight);
        $orderStatus.combobox({
            editable: false,
            width:130,
            url: abp.appPath + 'api/services/BXJGShop/CommonOrder/GetAllOrderStatus',
            queryParams: { forType: 1 },
            prompt:abp.localization.bxjgShop('BXJG.Shop.Sale.OrderStatus'),
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        var $paymentStatus = $('<input />').appendTo($toolbarRight);
        $paymentStatus.combobox({
            editable: false,
            width:130,
            url: abp.appPath + 'api/services/BXJGShop/CommonOrder/GetAllPaymentStatus',
            queryParams: { forType: 1 },
            prompt:abp.localization.bxjgShop('BXJG.Shop.Sale.PaymentStatus'),
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        var $logisticsStatusStatus = $('<input />').appendTo($toolbarRight);
        $logisticsStatusStatus.combobox({
            editable: false,
            width:130,
            url: abp.appPath + 'api/services/BXJGShop/CommonOrder/GetAllLogisticsStatus',
            queryParams: { forType: 1 },
            prompt:abp.localization.bxjgShop('BXJG.Shop.Sale.LogisticsStatus'),
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        var $paymentMethodCombobox = $('<input />').appendTo($toolbarRight);
        $paymentMethodCombobox.combobox({
            editable: false,
            width:130,
            url: abp.appPath + 'api/services/BXJGShop/Dictionary/GetNodesForSelect',
            queryParams: { ParentId: 3, forType: 1 },
            prompt: abp.localization.bxjgShop('BXJGShopPaymentMethod'),
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        var $distributionMethodCombobox = $('<input />').appendTo($toolbarRight);
        $distributionMethodCombobox.combobox({
            editable: false,
            width:130,
            url: abp.appPath + 'api/services/BXJGShop/Dictionary/GetNodesForSelect',
            queryParams: { ParentId: 4, forType: 1 },
            prompt: abp.localization.bxjgShop('BXJGShopDistributionMethod'),
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        function search() {
            var data = {};
            var keywords = $keywordTextbox.textbox('getValue');
            if (keywords)
                data.keywords = keywords;
            var brandId = $orderStatus.combobox('getValue');
            if (brandId)
                data.brandId = brandId;
            $dg.datagrid('load', data);
        }

        //str:删除 审核，注意本地化
        function getSelected(str) {
            var rows = $dg.datagrid('getSelections');
            if (rows.length == 0) {//若没有选择，返回的是一个空数组，而非undifined/null
                abp.message.warn(abp.localization.zlj('PleaseSeleteLines', $.trim(str)));
                return false;
            }
            return rows;
        }
        //只有新增和修改会使用编辑表单页面，批量审核或其它需要表单的操作应该另外定义方法而不是高度抽象一个表单窗口
        //em：编辑时的id 若不传则新增
        function showForm(id) {
            var $dlg = $('<div id="dlg' + m + '" />');
            if (id)
                $dlg.data('id', id);

            var clsSeclected = $cls.tree('getSelected');
            if (clsSeclected)
                $dlg.data('clsId', clsSeclected.id);

            $dlg.data('onAdded', function () {
                $dlg.dialog('close');
                $dg.datagrid('load');
            });
            $dlg.data('onUpdated', function () {
                $dlg.dialog('close');
                $dg.datagrid('reload');
            });

            $dlg.dialog({
                iconCls: id ? $btnUpdateOptions.iconCls : $btnAddOptions.iconCls,
                title: (id ? $btnUpdateOptions.text : $btnAddOptions.text) + '订单',//abp.localization.bxjgShop(m),
                width: 650,
                height: 520,
                border: false,
                modal: true,
                href: '/BXJGShop/' + m + '/form.html?_t=' + abp.pageLoadTime.getTime(),
                buttons: [{
                    iconCls: 'icon-ok',
                    text: abp.localization.zlj('Save'),
                    handler: function () {
                        var callback = $dlg.data('clickSave');
                        if (callback)
                            callback();
                    }
                }, {
                    iconCls: 'icon-undo',
                    text: abp.localization.zlj('Back'),
                    handler: function () {
                        $dlg.dialog('close');
                    }
                }],
                onClose: function () {
                    $dlg.dialog('destroy');
                }
                //可以通过onload事件初始化控件，如：下拉框
            });
        }

        $dg.datagrid({
            ctrlSelect: true,
            fit: true,
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: false,
            border: false,
            showFooter1: true,
            rownumbers: true,
            pagination: true,
            toolbar: '#dgToolbar' + m,
            //striped: true,
            ///api/services/app/BXJGShopOrder/GetAll
            url: abp.appPath + 'api/services/app/BXJGShopOrder/GetAll',
            queryParams: {
                //"CategoryId": 1,
                //"Published": true
            },
            frozenColumns: [[
                { field: 'ck', checkbox: true },
                //{
                //    field: 'image', title: '封面图', align: 'center', width: 100, formatter: function (value, row, index) {
                //        return '<img style="height:80px;" src="' + abp.appPath + '\\' + value + '" />';
                //    }
                //},
                { field: 'orderNo', title: '单号', width: 270, sortable: false }
            ]],
            columns: [[
                { field: 'customerUserName', title: '顾客', width:100, sortable: true },
                { field: 'orderTime', title: '下单时间', width:200, sortable: true },
                { field: 'statusText', title: '订单状态' },
                { field: 'merchandiseSubtotal', title: '商品小计', sortable: true, align: 'right' },
                { field: 'paymentAmount', title: '订单金额', sortable: true, align: 'right' },
                { field: 'integral', title: '积分小计', sortable: true, align: 'right' },
                { field: 'paymentMethodDisplayName', title: '支付方式', sortable: true, align: 'right' }//,
                //{ field: 'published', title: '是否发布', align: 'center', formatter: abp.easyui.datagridBool },
                //{ field: 'availableStart', title: '发布开始时间' },
                //{ field: 'availableEnd', title: '发布结束时间' },
                //{ field: 'hot', title: '热卖', align: 'center', formatter: abp.easyui.datagridBool },
                //{ field: 'new', title: '新品', align: 'center', formatter: abp.easyui.datagridBool },
                //{ field: 'home', title: '首页显示', align: 'center', formatter: abp.easyui.datagridBool },
                //{ field: 'focus', title: '首页轮播', align: 'center', formatter: abp.easyui.datagridBool }
            ]],
            onRowContextMenu: function (e, index, row) {
                if (!row)
                    return;
                e.preventDefault();//禁止浏览器默认右键菜单
                //可以在这里临时创建右键菜单，则可以通过闭包访问row
                var rows = $dg.datagrid('getSelections');
                if (rows.length == 1)
                    $dg.datagrid('clearSelections')
                $dg.datagrid('selectRow', index);
                $cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onDblClickRow: function (index, row) {
                if ($btnUpdate)
                    $btnUpdate.trigger('click');
            }
        });

    }());
</script>